package com.zonta.finance.serivce.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.common.utils.DateUtil;
import com.zonta.finance.config.ObjectConvertor;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.CreateCurrencyConfigForm;
import com.zonta.finance.domain.form.DelCurrencyConfigForm;
import com.zonta.finance.domain.form.UpdateCurrencyConfigForm;
import com.zonta.finance.domain.po.CurrencyConfig;
import com.zonta.finance.domain.vo.GetCurrencyConfigVo;
import com.zonta.finance.domain.vo.ListCurrencyConfigVo;
import com.zonta.finance.mapper.CurrencyConfigMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.serivce.ICurrencyConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 货币配置 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-29
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CurrencyConfigServiceImpl extends ServiceImpl<CurrencyConfigMapper, CurrencyConfig> implements ICurrencyConfigService {
    private final ObjectConvertor objectConvertor;
    private final TokenService<AdminDTO> tokenService;

    /**
     * 添加币别
     * @param form
     * @return
     */
    @Override
    public Boolean createByFrom(CreateCurrencyConfigForm form) {
        if (ObjUtil.isNotEmpty(getByCode(form.getCode()))){
            throw new BizException("币别已存在");
        }
        CurrencyConfig currencyConfig = objectConvertor.toCurrencyConfig(form);
        currencyConfig.initDefault();
        currencyConfig.setMemberId(tokenService.getThreadLocalUserId());
        currencyConfig.setTenantId(tokenService.getThreadLocalTenantId());
        return save(currencyConfig);
    }
    /**
     * 删除币别
     *
     * @param form
     * @return
     */
    @Override
    public boolean del(DelCurrencyConfigForm form) {
        LambdaUpdateWrapper<CurrencyConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CurrencyConfig::getDelFlag,true)
                .set(CurrencyConfig::getTenantId, tokenService.getThreadLocalUserId())
                .set(CurrencyConfig::getUpdateTime,DateUtil.getSystemTime())
                .eq(CurrencyConfig::getTenantId,tokenService.getThreadLocalTenantId())
                .eq(CurrencyConfig::getBaseCurrencyFlag,false)
                .eq(CurrencyConfig::getUseCount,0)
                .eq(CurrencyConfig::getId,form.getId());

        if (!update(wrapper)) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Override
    public List<ListCurrencyConfigVo> lists() {
        LambdaQueryWrapper<CurrencyConfig> wrapper = new LambdaQueryWrapper<>();

        wrapper.select(CurrencyConfig::getId,CurrencyConfig::getCode,CurrencyConfig::getName,CurrencyConfig::getExchangeRate,CurrencyConfig::getBaseCurrencyFlag,CurrencyConfig::getUseCount)
                .orderByDesc(CurrencyConfig::getBaseCurrencyFlag)
                .orderByAsc(CurrencyConfig::getCode);
        List<CurrencyConfig> list = list(wrapper);
        return objectConvertor.toListCurrencyConfigVo(list);
    }

    /**
     * 获取币别详细信息
     * @param id
     * @return
     */
    @Override
    public GetCurrencyConfigVo getGetCurrencyConfigVoById(Long id) {
        LambdaQueryWrapper<CurrencyConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CurrencyConfig::getId,id)
                .eq(CurrencyConfig::getTenantId,tokenService.getThreadLocalTenantId());
        CurrencyConfig currencyConfig = baseMapper.selectOne(wrapper);
        return objectConvertor.toGetCurrencyConfigVo(currencyConfig);

    }

    /**
     * 修改币别
     * @param form
     * @return
     */
    @Override
    public Boolean updateByFrom(UpdateCurrencyConfigForm form) {
        GetCurrencyConfigVo getCurrencyConfigVo = getByCode(form.getCode());
        if (getCurrencyConfigVo != null
                && !Objects.equals(form.getId(), getCurrencyConfigVo.getId())) {
            throw new BizException("币别已存在");
        }
        LambdaUpdateWrapper<CurrencyConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CurrencyConfig::getCode,form.getCode())
                .set(CurrencyConfig::getName,form.getName())
                .set(CurrencyConfig::getExchangeRate,form.getExchangeRate())
                .set(CurrencyConfig::getUpdateMemberId,tokenService.getThreadLocalUserId())
                .set(CurrencyConfig::getUpdateTime,DateUtil.getSystemTime())
                .eq(CurrencyConfig::getId,form.getId())
                .eq(CurrencyConfig::getTenantId,tokenService.getThreadLocalTenantId());
        return update(wrapper);
    }


    /**
     * 查询币别信息
     *
     * @param code
     * @return
     */
    private GetCurrencyConfigVo getByCode(String code) {
        LambdaQueryWrapper<CurrencyConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CurrencyConfig::getId,CurrencyConfig::getCode,CurrencyConfig::getExchangeRate,CurrencyConfig::getBaseCurrencyFlag)
                .eq(CurrencyConfig::getCode,code)
                .eq(CurrencyConfig::getTenantId,tokenService.getThreadLocalTenantId());
        CurrencyConfig one = getOne(wrapper);
        return objectConvertor.toGetCurrencyConfigVo(one);
    }
}
